package com.itheima.media.xxl.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.media.mapper.WmNewsMapper;
import com.itheima.media.pojo.WmNews;
import com.itheima.media.service.WmNewsService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author：shenkunlin
 * @date： 2021/8/27 11:01
 * @Description：
 ***/
@Component
@Slf4j
public class ArticleSyncTask {

    @Autowired
    private WmNewsMapper wmNewsMapper;



    @Autowired
    private WmNewsService wmNewsService;

    /***
     * 创建任务
     *  1：@XxlJob(value="syncArticle")
     *  2：要执行的任务
     *  3：return ReturnT<String>
     */
    @XxlJob(value="syncArticle")
    public ReturnT<String> syncArticle(String param){
        //1：查询所有状态为8，并且发布时间<=当前时间    使用分页（演示案例为了方便不使用分页）
        QueryWrapper<WmNews> queryWrapper = new QueryWrapper<WmNews>();
        queryWrapper.eq("status",8);
        queryWrapper.le("publish_time", LocalDateTime.now());
        List<WmNews> wmNews = wmNewsMapper.selectList(queryWrapper);

        if(wmNews!=null && wmNews.size()>0){
            //2：修改状态9（发布）
            //3：同步
            wmNewsService.batchModify(wmNews);
        }
        return ReturnT.SUCCESS;
    }

}
